//
//  CardRecordViewController.m
//  FuncFanc
//
//  Created by careate-hubing on 2020/12/16.
//  Copyright © 2020 huhu. All rights reserved.
//

#import "CardRecordViewController.h"
#import "QHPickerView.h"
#import "UserRecordViewController.h"

@interface CardRecordViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableview;

@property (nonatomic,strong) NSArray *titleArray;

@property (nonatomic,strong) UITextField *cardNumberTF;

@property (nonatomic,strong) UILabel *bankLab;

@property (nonatomic,strong) UITextField *allCreditLineTF;

@property (nonatomic,strong) UILabel *billDateLab;

@property (nonatomic,strong) UILabel *paybackDateLab;

@property (nonatomic,strong) UILabel *userName;

@property (nonatomic,strong) QHPickerView *billDatePicker;

@property (nonatomic,strong) QHPickerView *paybackDatePicker;

@property (nonatomic,strong) QHPickerView *bankPicker;

@property (nonatomic,strong) QHPickerView *userPicker;

@end

@implementation CardRecordViewController

-(NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray = @[@"卡号",@"银行",@"总额度",@"账单日",@"还款日",@"户主"];
    }
    return _titleArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createSubviews];
    self.navigationItem.title = @"卡片信息";
}

-(void)createSubviews
{
    _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self.view addSubview:_tableview];
    _tableview.tableFooterView = [self createTableviewFooter];
    [_tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return 50;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([UITableViewCell class])];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@",self.titleArray[indexPath.row]];
    switch (indexPath.row) {
        case 0:
        {
            if (!_cardNumberTF) {
                _cardNumberTF = [[UITextField alloc]init];
                _cardNumberTF.placeholder = @"请输入卡号";
                _cardNumberTF.keyboardType = UIKeyboardTypeNumberPad;
                [cell.contentView addSubview:_cardNumberTF];
                [_cardNumberTF mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo(-20);
                    make.left.mas_equalTo(120);
                    make.height.equalTo(cell.contentView);
                    make.centerY.equalTo(cell.contentView);
                }];
            }
        }
            break;
        case 1:
        {
            if (!_bankLab) {
                _bankLab = [[UILabel alloc]init];
                _bankLab.text = @"点击选择银行";
                _bankLab.textColor = [UIColor lightGrayColor];
                _bankLab.font = [UIFont systemFontOfSize:13];
                [cell.contentView addSubview:_bankLab];
                [_bankLab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo(-20);
                    make.left.mas_equalTo(120);
                    make.height.equalTo(cell.contentView);
                    make.centerY.equalTo(cell.contentView);
                }];
            }
        }
            break;
        case 2:
        {
            if (!_allCreditLineTF) {
                _allCreditLineTF = [[UITextField alloc]init];
                _allCreditLineTF.placeholder = @"请输入总额度";
                _allCreditLineTF.keyboardType = UIKeyboardTypeNumberPad;
                [cell.contentView addSubview:_allCreditLineTF];
                [_allCreditLineTF mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo(-20);
                    make.left.mas_equalTo(120);
                    make.height.equalTo(cell.contentView);
                    make.centerY.equalTo(cell.contentView);
                }];
            }
        }
            break;
        case 3:
        {
            if (!_billDateLab) {
                _billDateLab = [[UILabel alloc]init];
                _billDateLab.text = @"点击选择账单日";
                _billDateLab.textColor = [UIColor lightGrayColor];
                _billDateLab.font = [UIFont systemFontOfSize:13];
                [cell.contentView addSubview:_billDateLab];
                [_billDateLab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo(-20);
                    make.left.mas_equalTo(120);
                    make.height.equalTo(cell.contentView);
                    make.centerY.equalTo(cell.contentView);
                }];
            }
        }
            break;
        case 4:
        {
            if (!_paybackDateLab) {
                _paybackDateLab = [[UILabel alloc]init];
                _paybackDateLab.text = @"点击选择还款日";
                _paybackDateLab.textColor = [UIColor lightGrayColor];
                _paybackDateLab.font = [UIFont systemFontOfSize:13];
                [cell.contentView addSubview:_paybackDateLab];
                [_paybackDateLab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo(-20);
                    make.left.mas_equalTo(120);
                    make.height.equalTo(cell.contentView);
                    make.centerY.equalTo(cell.contentView);
                }];
            }
        }
            break;
        case 5:
        {
            if (!_userName) {
                _userName = [[UILabel alloc]init];
                _userName.text = @"点击选择信用卡用户";
                _userName.textColor = [UIColor lightGrayColor];
                _userName.font = [UIFont systemFontOfSize:13];
                [cell.contentView addSubview:_userName];
                [_userName mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo(-20);
                    make.left.mas_equalTo(120);
                    make.height.equalTo(cell.contentView);
                    make.centerY.equalTo(cell.contentView);
                }];
            }
        }
            break;
       
        default:
            break;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.allCreditLineTF resignFirstResponder];
    [self.cardNumberTF resignFirstResponder];
    NSString *titleStr = self.titleArray[indexPath.row];
    if ([titleStr isEqualToString:@"账单日"])
    {
        [self.billDatePicker showWithIndex:15];
    }
    else if([titleStr isEqualToString:@"还款日"])
    {
        [self.paybackDatePicker showWithIndex:15];
    }
    else if([titleStr isEqualToString:@"银行"])
    {
        [self.bankPicker showWithIndex:20];
    }
    else if([titleStr isEqualToString:@"户主"])
    {
        [self.userPicker showWithIndex:0];
    }
}

-(UIView *)createTableviewFooter{
    UIView *footer = [[UIView alloc]init];
    footer.frame = CGRectMake(0, 0, ScreenWidth, 120);
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [submitBtn addTarget:self action:@selector(submitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [submitBtn setBackgroundImage:[UIImage imageNamed:@"careateBlueBtnBG"] forState:UIControlStateNormal];;
    submitBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [footer addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(footer);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(40);
    }];
    return footer;
}

-(QHPickerView *)paybackDatePicker
{
    __weak typeof(self) ws = self;
    if (!_paybackDatePicker) {
        _paybackDatePicker = [QHPickerView selectData:^(NSString * _Nonnull timeStr, NSInteger index) {
            ws.paybackDateLab.text = [NSString stringWithFormat:@"每月%@",timeStr];
        }];
    }
    return _paybackDatePicker;
}

-(QHPickerView *)billDatePicker{
    if (!_billDatePicker) {
        __weak typeof(self) ws = self;
        _billDatePicker = [QHPickerView selectData:^(NSString * _Nonnull timeStr, NSInteger index) {
            ws.billDateLab.text = [NSString stringWithFormat:@"每月%@",timeStr];
        }];
        [self.view addSubview:_billDatePicker];
    }
    return _billDatePicker;
}

-(QHPickerView *)bankPicker
{
    __weak typeof(self) ws = self;
    if (!_bankPicker) {
        _bankPicker = [QHPickerView selectData:^(NSString * _Nonnull timeStr, NSInteger index) {
            ws.bankLab.text = [NSString stringWithFormat:@"%@",timeStr];
        }];
        _bankPicker.dataSource = [QHCommonTool getCommonBanks];
    }
    return _bankPicker;
}

-(QHPickerView *)userPicker
{
    __weak typeof(self) ws = self;
    if (!_userPicker) {
        _userPicker = [QHPickerView selectData:^(NSString * _Nonnull timeStr, NSInteger index) {
            if ([timeStr isEqualToString:@"新增用户"])
            {
                // push 用户页面
                UserRecordViewController *vc = [[UserRecordViewController alloc]init];
                
                [self.navigationController pushViewController:vc animated:YES];
                
            }
            else
            {
                ws.userName.text = [NSString stringWithFormat:@"%@",timeStr];
            }
        }];
        LKDBHelper *globalHelper = [LKDBHelper getUsingLKDBHelper];
        NSArray *result = [globalHelper search:[UserModel class] where:nil orderBy:nil offset:0 count:INT_MAX];
        NSMutableArray *temp = [NSMutableArray array];
        for (UserModel *user in result) {
            [temp addObject:user.nickName];
        }
        [temp addObject:@"新增用户"];
        _userPicker.dataSource = temp;
    }
    return _userPicker;
}

-(void)submitBtnClick:(UIButton *)aBtn
{
    CreditCardModel *card = [CreditCardModel new];
    
    LKDBHelper *globalHelper = [LKDBHelper getUsingLKDBHelper];
    UserModel *user = nil;
    NSArray *result = [globalHelper search:[UserModel class] where:@{@"nickName":_userName.text?:@""} orderBy:nil offset:0 count:INT_MAX];
    if (result.count > 0) {
        user = result.firstObject;
    }
   
    card.cardUser = user;
    card.bankName = self.bankLab.text;
    card.cardNumber = self.cardNumberTF.text;
    card.allCreditLine = self.allCreditLineTF.text;
    card.paybackDate = _paybackDateLab.text;
    card.billDate = _billDateLab.text;
    
    [globalHelper insertToDB:card];
    [[HUDHelper sharedInstance]loading:@"卡片信息保存成功" delay:1.5 execute:nil completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
@end
